package app.hoangkien.quanlycongviec;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore.Audio;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TimePicker;

public class Add_Works extends Activity implements OnClickListener {
	private Spinner loaicongviec, laplai, chuongtruoc, chuongchinh;
	private Button trolai, thoat, nhaplai, ok, cancel,time,ngay;
	private EditText tencongviec,motacongviec, pass, solanbao, khoangcach;
	Calendar cal;
	Date dateFinish;
	Date hourFinish;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add__works);
		
		loaicongviec = (Spinner) findViewById(R.id.edittext3);
		spinerloaicongviec();
		laplai = (Spinner) findViewById(R.id.edittext4);
		spinerlaplai();
		chuongtruoc = (Spinner) findViewById(R.id.chuongtruoc);
		spineraudio(chuongtruoc);
		chuongchinh = (Spinner) findViewById(R.id.chuongchinh);
		spineraudio(chuongchinh);
		
		trolai = (Button) findViewById(R.id.button1);
		trolai.setOnClickListener(this);
		thoat = (Button) findViewById(R.id.button2);
		thoat.setOnClickListener(this);
		nhaplai = (Button) findViewById(R.id.nhaplai);
		nhaplai.setOnClickListener(this);
		ok = (Button) findViewById(R.id.ok);
		ok.setOnClickListener(this);
		cancel = (Button) findViewById(R.id.cancel);
		cancel.setOnClickListener(this);
		time = (Button) findViewById(R.id.thoigian);
		time.setOnClickListener(this);
		ngay = (Button) findViewById(R.id.ngay);
		ngay.setOnClickListener(this);
		
		tencongviec = (EditText) findViewById(R.id.edittext1);
		motacongviec = (EditText) findViewById(R.id.edittext2);
		pass = (EditText) findViewById(R.id.edittext5);
		solanbao = (EditText) findViewById(R.id.solanbao);
		khoangcach = (EditText) findViewById(R.id.khoangcach);
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			//xu ly nhan vao trolai
			Intent intent = new Intent(Add_Works.this, MainActivity.class);
			startActivity(intent);
			break;
		case R.id.button2:
			//xu ly nhan vao thoat
			intent = new Intent(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_HOME);
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(intent);
			break;
		case R.id.nhaplai:
			//xu ly nhan vao nhaplai
			tencongviec.setText("");
			motacongviec.setText("");
			pass.setText("");
			solanbao.setText("");
			khoangcach.setText("");
			break;
		case R.id.ok:
			//xu ly nhan vao ok
			break;
		case R.id.cancel:
			//xu ly nhan vao cancel
			finish();
			break;
		case R.id.thoigian:
			// xu ly nhan vao time
			getDefaultInfor();
			showTimePickerDialog();
			break;
		case R.id.ngay:
			//xu ly nhan vao ngay
			getDefaultInfor();
			showDatePickerDialog();
			break;

		default:
			break;
		}
	}
	
	public void spinerloaicongviec() {
		List<String> list = new ArrayList<String>();
		list.add("Tất cả");
		list.add("Bản Thân");
		list.add("Gia Đình");
		list.add("Bạn Bè");
		list.add("Công Việc Cá Nhân");
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
				Add_Works.this,
				android.R.layout.simple_spinner_dropdown_item, list);
		loaicongviec.setAdapter(dataAdapter);
	}
	public void spinerlaplai() {
		List<String> list = new ArrayList<String>();
		list.add("lap lai theo ngay");
		list.add("lap lai theo tháng");
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
				Add_Works.this,
				android.R.layout.simple_spinner_dropdown_item, list);
		laplai.setAdapter(dataAdapter);
	}
	
	public void spineraudio(Spinner sp) {
		Cursor imageCursor;
		imageCursor = this.managedQuery(
				android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				null, null, null, null);
		startManagingCursor(imageCursor);
		String[] columnsToMap = new String[] { Audio.Media.DISPLAY_NAME,
				Audio.Media.SIZE };
		int[] mapTo = new int[] { android.R.id.text1, android.R.id.text2 };
		ListAdapter mAdapter = new SimpleCursorAdapter(this,
				android.R.layout.simple_spinner_dropdown_item, imageCursor,
				columnsToMap, mapTo);
		sp.setAdapter((SpinnerAdapter) mAdapter);
	}
	public void showTimePickerDialog() {
		OnTimeSetListener callback = new OnTimeSetListener() {
			public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
				String s = hourOfDay + ":" + minute;
				int hourTam = hourOfDay;
				if (hourTam > 12)
					hourTam = hourTam - 12;
				time.setText(hourTam + ":" + minute
						+ (hourOfDay > 12 ? " PM" : " AM"));
				time.setTag(s);
				cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
				cal.set(Calendar.MINUTE, minute);
				hourFinish = cal.getTime();
			}
		};
		String s = time.getTag() + "";
		String strArr[] = s.split(":");
		int gio = Integer.parseInt(strArr[0]);
		int phut = Integer.parseInt(strArr[1]);
		TimePickerDialog time = new TimePickerDialog(Add_Works.this,
				callback, gio, phut, true);
		time.setTitle("Chọn giờ hoàn thành !!!");
		time.show();
	}

	public void showDatePickerDialog() {
		OnDateSetListener callback = new OnDateSetListener() {
			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				// Date
				ngay.setText((dayOfMonth) + "/" + (monthOfYear + 1) + "/"
						+ year);
				cal.set(year, monthOfYear, dayOfMonth);
				dateFinish = cal.getTime();
			}
		};
		String s = ngay.getText() + "";
		String strArrtmp[] = s.split("/");
		int ngay = Integer.parseInt(strArrtmp[0]);
		int thang = Integer.parseInt(strArrtmp[1]) - 1;
		int nam = Integer.parseInt(strArrtmp[2]);
		DatePickerDialog pic = new DatePickerDialog(Add_Works.this, callback,
				nam, thang, ngay);
		pic.setTitle("Chá»�n ngÃ y hoÃ n thÃ nh");
		pic.show();
	}

	public void getDefaultInfor() {
		cal = Calendar.getInstance();
		SimpleDateFormat dft = null;
		dft = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
		String strDate = dft.format(cal.getTime());
		ngay.setText(strDate);
		dft = new SimpleDateFormat("hh:mm a", Locale.getDefault());
		String strTime = dft.format(cal.getTime());
		time.setText(strTime);
		dft = new SimpleDateFormat("HH:mm", Locale.getDefault());
		time.setTag(dft.format(cal.getTime()));

		dateFinish = cal.getTime();
		hourFinish = cal.getTime();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.add__works, menu);
		return true;
	}
	

}
